<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="时间段" name="name">
						<a-input v-model:value="searchFormState.name" placeholder="请输入时间段" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="所属机构" name="orgId">
						<a-tree-select
							v-model:value="searchFormState.orgId"
							placeholder="请选择所属机构"
							:tree-data="orgTreeData"
							:tree-default-expanded-keys="treeDefaultExpandedKeys"
							tree-node-filter-prop="title"
							show-search
							allow-clear
							@change="onOrgChange"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="会议室类型" name="meetingId">
						<a-select
							v-model:value="searchFormState.devicetypeId"
							placeholder="请输入设备类型"
							:options="typeOptions"
							:disabled="!searchFormState.orgId"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="4" v-show="advanced">
					<a-form-item label="状态" name="disable">
						<a-select v-model:value="searchFormState.disable" placeholder="请选择是否禁用" :options="disableOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('meetingTimeAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-button
						v-if="hasPerm('meetingTimeBatchDelete')"
						buttonName="批量删除"
						icon="DeleteOutlined"
						buttonDanger
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchMeetingTime"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'disable'">
					{{ $TOOL.dictTypeData('COMMON_STATUS', record.disable) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('meetingTimeEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['meetingTimeEdit', 'meetingTimeDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteMeetingTime(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('meetingTimeDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="meetingtime">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import meetingTimeApi from '@/api/biz/meetingTimeApi'
	import bizUserApi from "@/api/biz/bizUserApi";
	import deviceTypeApi from "@/api/biz/deviceTypeApi";
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '所属机构',
			dataIndex: 'orgName'
		},
		{
			title: '设备类型名称',
			dataIndex: 'deviceTypeName'
		},
		{
			title: '时间段',
			dataIndex: 'name'
		},
		{
			title: '状态',
			dataIndex: 'disable'
		},
		{
			title: '排序码',
			dataIndex: 'sortCode'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['meetingTimeEdit', 'meetingTimeDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	const orgTreeData = ref([])
	const treeDefaultExpandedKeys = ref([])
	const typeOptions = ref([])

	// 获取机构树数据
	const getOrgTree = () => {
		bizUserApi.userOrgTreeSelector().then((res) => {
			if (res !== null) {
				// 处理树状数据，禁用 parentId = "0" 的节点
				const processTreeData = (nodes) => {
					return nodes.map((node) => {
						const processedNode = {
							title: node.name,
							value: node.id,
							key: node.id,
							disabled: node.parentId === '0', // 禁用根节点
							children: node.children ? processTreeData(node.children) : undefined
						}
						return processedNode
					})
				}

				orgTreeData.value = processTreeData(res)

				// 默认展开前两级
				res.forEach((item) => {
					if (item.parentId === '0') {
						treeDefaultExpandedKeys.value.push(item.id)
						if (item.children) {
							item.children.forEach((child) => {
								treeDefaultExpandedKeys.value.push(child.id)
							})
						}
					}
				})
			}
		})
	}

	// 机构变化时获取对应的设备类型
	// const onOrgChange = (orgId) => {
	// 	searchFormState.value.typeId = undefined
	// 	typeOptions.value = []
	//
	// 	if (orgId) {
	// 		deviceTypeApi.getAll().then((res) => {
	// 			if (res && res.length > 0) {
	// 				// 找到选中机构对应的设备类型
	// 				const orgNode = res.find((item) => item.id === orgId && item.isOrg)
	// 				if (orgNode && orgNode.children) {
	// 					typeOptions.value = orgNode.children.map((child) => ({
	// 						label: child.name,
	// 						value: child.id
	// 					}))
	// 				}
	// 			}
	// 		})
	// 	}
	// }

	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return meetingTimeApi.meetingTimePage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteMeetingTime = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		meetingTimeApi.meetingTimeDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchMeetingTime = (params) => {
		meetingTimeApi.meetingTimeDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	const disableOptions = tool.dictList('COMMON_STATUS')
</script>
